from rest_framework.views import APIView
from rest_framework import generics
from users.models import User
from meiduo_admin.serializers.users import UserViewSerializer
from meiduo_admin.utils.pagination import MeiduoPagination

from rest_framework.response import Response
from rest_framework.permissions import IsAdminUser


# class UserView(APIView):

#     def get(self, request):
#         queryset = User.objects.filter(is_staff=False).order_by('-id')
#         serializer = UserViewSerializer(queryset,many=True)
#         return Response(serializer.data)


class UserView(generics.ListCreateAPIView):
    # permission_classes = [IsAdminUser]
    # queryset = User.objects.filter(is_staff=True)
    def get_queryset(self):
        queryset = User.objects.filter(is_staff=False)
        keyword = self.request.query_params.get('keyword')
        if keyword:
            queryset = queryset.filter(username__contains=keyword)
        queryset = queryset.order_by('-id')
        return queryset

    serializer_class = UserViewSerializer
    pagination_class = MeiduoPagination
